/**
 * @fileOverview Skill classes
 * @requires app
 */

(function() {
	/**
	 * @class Basic skill class
	 * @extends app.Basic
	 * @borrows app.Basic.extend as extend
	 *
	 * @property {String} name Skill name
	 * @property {Number} level Skill level
	 * @property {Number} [chance=100] Chance to hit
	 * @property {app.Effect[]} effects List of effects applied by this skill
	 */
	app.Skill = app.Basic.extend({
		/** @lends app.Skill.prototype */
		
		/**
		 * Skill initialization
		 *
		 * @param {Object} options Skill configuration
		 */
		init : function(options) {
			options || (options = {});
			
			this.chance = 100;
			
			for (var key in options) {
				switch (key) {
					// Effects
					case 'effects':
						this.effects = [];
						
						for (var i = 0; i < options[key].length; i++) {
							this.effects.push(app.EffectMgr.create(options[key][i]));
						}
						
						break;
					// Level-up this skill
					case 'level':
						this.level = options[key];
						break;
					// Set other skill properties
					default:
						this[key] = options[key];
				}
			}
		},
		
		/**
		 * Get a chance that this skill will trigger
		 *
		 * @returns {Number} Chance that current skill will trigger
		 */
		getChance : function() {
			return this.chance;
		},
		
		/**
		 * Get string representation of skill
		 *
		 * @return {String} String representation
		 */
		toString : function() {
			return '[' + this.level + '] ' + this.name;
		}
	});
	
	/* ************************************************************************************* */
	
	/**
	 * @class Collection of skills
	 * @extends app.Collection
	 * @borrows app.Basic.extend as extend
	 *
	 * @property {app.Skill[]} items Array of skills
	 */
	app.SkillsCollection = app.Collection.extend({
		/** @lends app.SkillsCollection.prototype */
		
		/**
		 * Get random skill from collection
		 *
		 * @returns {app.Skill} Random skill
		 */
		getRandom : function() {
			var chance, skills;
			
			chance = Math.round(Math.random() * 100);
			skills = this.filter(function(skill) {
				return skill.getChance() >= chance;
			});
			
			return this.constructor.__super__.getRandom.call(skills);
		}
	});
	
	/* ************************************************************************************* */
	
	/**
	 * @class Skill manager
	 * @extends app.Manager
	 */
	app.SkillMgr = new app.Manager(app.Skill);
})();